package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.interactive.UserInteractive;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.InteractiveModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.gameplayer.module.xiaomian.XiaoMianModule;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_CROSS_GIVE_LIKE_RESP, desc = "跨服点赞玩家响应")
public class CrossGiveLikePlayerRespCmd extends AbstractCommand {
	PlayerProto.CrossGiveLikeRespMsg reqMsg;
	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = PlayerProto.CrossGiveLikeRespMsg.parseFrom(packet.getBytes());
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		InteractiveModule interactiveModule = player.getModule(InteractiveModule.class);
		interactiveModule.removeOpLikeUserId(reqMsg.getLikeUserId());
		Property consume = new Property(GameConfig.GAME_ITEM_LIKE, BigInteger.ONE);
		if(reqMsg.getIsPreReduce()){
			player.getModule(PreReduceModule.class).restorePreReduce(consume);
		}
		UserInteractive userInteractive = interactiveModule.getUserInteractive();
		if(1 == reqMsg.getType()){//扣道具
			if(reqMsg.getRet() == 0){
				if (userInteractive.getLikeCount() >= GameConfig.GIVE_LIKE_MAX_COUNT) {//没有点赞次数了
					player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.System, eLogMoneyType.GiveLike);
				}
				userInteractive.addLikeUserId(reqMsg.getLikeUserId());
				userInteractive.setTotalGiveLikeTimes(userInteractive.getTotalGiveLikeTimes() + 1);
				player.notifyListener(eGamePlayerEventType.GiveLikeTotalTimes.getValue(), userInteractive);
				player.notifyListener(eGamePlayerEventType.TotalGiveLikeTime.getValue(), 1);
				player.getModule(InteractiveModule.class).syncUserInteractive();
				//平台数据加一下
				player.getModule(XiaoMianModule.class).addGiveLikeTimes(1);
			}
		}else {//扣次数
			if(reqMsg.getRet() == 0){
				userInteractive.addLikeUserId(reqMsg.getLikeUserId());
				userInteractive.setTotalGiveLikeTimes(userInteractive.getTotalGiveLikeTimes() + 1);
				player.notifyListener(eGamePlayerEventType.GiveLikeTotalTimes.getValue(), userInteractive);
				player.notifyListener(eGamePlayerEventType.TotalGiveLikeTime.getValue(), 1);
				player.getModule(InteractiveModule.class).syncUserInteractive();
				//平台数据加一下
				player.getModule(XiaoMianModule.class).addGiveLikeTimes(1);
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		PlayerProto.GiveLikeRespMsg.Builder respMsg = PlayerProto.GiveLikeRespMsg.newBuilder();
		respMsg.setRet(reqMsg.getRet());
		if (0 == reqMsg.getRet()) {
			respMsg.setHeartCount(reqMsg.getHeartCount());
			respMsg.setEarnSpeed(reqMsg.getEarnSpeed());
			respMsg.setEffectValue(reqMsg.getEffectValue());
		}
		player.sendPacket(Protocol.U_GIVE_LIKE, respMsg);
	}
}
